package com.example.demo;

import com.example.demo.demos.dao.UserDao;
import com.example.demo.demos.entity.User;
import com.example.demo.demos.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication
public class DemoApplication {

    private static SqlSession sqlSession;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);

        mybatisDemo3();

    }

    private static void mybatisDemo(){
        try{
            sqlSession = MybatisUtils.getSqlSession();
            //方式一：getMapper
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            List<User> userList = mapper.getUserList();
            for (User user : userList) {
                System.out.println(user);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }
    private static void mybatisDemo2(){
        try{
            sqlSession = MybatisUtils.getSqlSession();
            //方式一：getMapper
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            int currentPage = 1;  //第几页
            int pageSize = 5;  //每页显示几个
            Map<String,Integer> map = new HashMap<String,Integer>();
            map.put("startIndex",(currentPage-1)*pageSize);
            map.put("pageSize",pageSize);
            List<User> userList = mapper.getPages(map);
            for (User user : userList) {
                System.out.println(user);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }
    private static void mybatisDemo3(){
        try{
            sqlSession = MybatisUtils.getSqlSession();
            //方式一：getMapper
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            List<User> userList = mapper.queryName("张");
            for (User user : userList) {
                System.out.println(user);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }

    private void jdbcBase(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/demo";
            String name = "root";
            String password = "Hr@202203";
            Connection connection = DriverManager.getConnection(url, name, password);
            String sql = "SELECT name, price FROM books;";
            Statement statement = connection.createStatement();
            ResultSet list = statement.executeQuery(sql);
            if (list != null) {
                System.out.println("数据库登录成功！！！！");
                while (list.next()) {
                    System.out.println("数据:Name:" + list.getString("name") + "  price:" + list.getInt("price"));
                }
            } else {
                System.out.println("数据库登录失败");
            }
            list.close();
            statement.cancel();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
